<template>
  <div :style="background" class="bgBackground">
    <el-form :rules="rules"  label-position="labelPosition" label-width="80px" ref="form" :model="loginForm" class="loginContainer">
      <h3 class="loginTitle">跨境电商平台登录</h3>
      <el-form-item label="用户名称" prop="username">
        <el-input type="text" v-model="loginForm.username" placeholder="请输入用户名"></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password">
        <el-input type="password" v-model="loginForm.password" show-password placeholder="请输入密码"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="code">
        <el-input type="text" auto-complete="false" v-model="loginForm.code" placeholder="请输入验证码"
                  style="width: 200px;margin-right: 5px"></el-input>
        <img alt src="codeUrl" @click="updateCode">
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit" style="width: 100%">登录</el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="onRegistry">立即注册</el-button>
        <el-button @click="forgetPassword">忘记密码</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>

export default {
  name: "Login",
  data() {
    return {
      background:{

        backgroundImage: 'url(' + require ('../../static/img/background.jpg') + ')',
        backgroundRepeat: 'no-repeat',
        backgroundSize: '100% 100%'
      },
      labelPosition: 'left',
      codeUrl:'',// '/controller?time'+new Date() 连后端controller接口，返回值为一张图片
      loginForm: {
        username: '',
        password: '',
        code:'',
      },
      rules: {
        username: [{required:true, message:'请输入用户名', trigger:'blur'}],
        password: [{required:true, message:'请输入密码', trigger:'blur'}],
        code: [{required:true, message:'请输入验证码', trigger:'blur'}]
      }
    }
  },
  methods: {
    updateCode(){
        this.codeUrl = '';
        // '/controller?time'+new Date() 连后端controller接口，返回值为一张图片
    },
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //连后端Login,controller接口,传3个参数过去,返回token,若成功响应，返回身份数据
          /*this.getRequest('/login',this.loginForm).then(res => {
            if(res)
            {
              //存储用户token和当前登录用户信息
              const tokenStr = res.obj.tokenHead + res.obj.token;
              window.sessionStorage.setItem("tokenStr",tokenStr);
            }
          });*/
          let user = {
            username:this.loginForm.username,
            password:this.loginForm.password,
            nickname:'test',
            phone:'18990990789',
            identity:'3'
          }
          window.sessionStorage.setItem("user",JSON.stringify(user));
          this.$router.replace("/");//返回首页
        } else {
          this.$message.error('信息错误，请重新输入')
          return false;
        }
      })
      },
    onRegistry(){
      this.$router.push('/registry');
    },
    forgetPassword(){
      this.$router.push('/forgetPass');
    }
    }
}
</script>

<style>
  .loginContainer{
    border-radius: 15px;
    margin: 100px auto;
    width: 350px;
    padding: 15px 35px 15px 35px;
    background: aliceblue;
    border: 1px solid #aaaaaa;
    box-shadow: 0 0 25px #cacaca;

  }
  .loginTitle{
    margin: 8px auto 40px auto;
    text-align: center;
  }
  .bgBackground{
    width: 100%;
    height:100%;
    position: fixed;
  }
</style>
